﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SubSonic;

namespace AppBox.admin
{
    public partial class dept_new : PageBase
    {
        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ExtAspNet.ActiveWindow.GetConfirmHidePostBackReference();

            BindDDL();
        }

        private void BindDDL()
        {
            // 绑定下拉列表
            XDeptCollection items = new Select().From<XDept>()
                .OrderAsc(XDept.SortIndexColumn.ColumnName)
                .ExecuteAsCollection<XDeptCollection>();
            List<MyDept> mys = XDeptHelper.GetMyDeptCollection(items);

            ResolveDDL<MyDept>(mys);

            // 绑定到下拉列表（启用模拟树功能）
            ddlParent.EnableSimulateTree = true;
            ddlParent.DataTextField = "Name";
            ddlParent.DataValueField = "Id";
            ddlParent.DataSimulateTreeLevelField = "TreeLevel";
            ddlParent.DataSource = mys;
            ddlParent.DataBind();

            // 选中根节点
            ddlParent.SelectedValue = "0";
        }

        #endregion

        #region Events

        private void SaveItem()
        {
            XDept item = new XDept();
            item.Name = tbxName.Text.Trim();
            item.SortIndex = Convert.ToInt32(tbxSortIndex.Text.Trim());
            item.ParentDeptId = Convert.ToInt32(ddlParent.SelectedValue);
            item.Remark = tbxRemark.Text.Trim();
            item.Save(User.Identity.Name);
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveItem();

            ExtAspNet.Alert.Show("添加成功！", String.Empty, ExtAspNet.ActiveWindow.GetHidePostBackReference());
        }

        #endregion

    }
}
